package com.xinxing.city.recoverycheck.sdk_ttys;


import static com.xinxing.city.recoverycheck.serialport.Serialzhi.hexStringToByteArray2;

import com.xinxing.city.recoverycheck.serialport.Serialzhi;

public class DecodeAcParser {



    public static void test() {

        try {

            // 构造一个收到的报文
            byte[] buffer = Hexs.HexToByte("0002931f0a" +
                    "aaac01000000000000000000000000000000000000090000010002931f0a"+
                    "aaac0100000000000000000000000"
            );


            byte[] buffer2 = Hexs.HexToByte("0000000000000090000010002931f0a" +
                    "aaac01000000000000000000000000000000000000090000010002931f0a"+
                    "aaac01000000000000000000000000000000000000090000010002931f0a"
            );
            byte[] buffer3 = Hexs.HexToByte("aaac010000000000000000000000f7e3c90500ee790a00000300010003a6ce0aaaac010000000000000000000000f7e3c90500ee792600000200010103bb0aaaac010000000000000000000000f7e3c90500ee794200000d0001000000000000000000000000cfeb0a");
            DecodeAcParser.unboxing(buffer,null);// 第一组未传输完整
            DecodeAcParser.unboxing(buffer2,null);// 第二组头部包涵上一条报文
            DecodeAcParser.unboxing(buffer3,null);// 第二组头部包涵上一条报文



        } catch (Exception e) {
            throw new RuntimeException(e);
        }



    }


    static byte[] laterErr1 =null;
    static byte[] laterErr2 =null;
    static byte[] laterErr3 =null;
    static byte[] laterErr4 =null;
    static byte[] laterErr5 =null;

    public static byte[] getErr(byte[] buffer) {

        buffer = Hexs.byteMerger(laterErr1,laterErr2,laterErr3,laterErr4,laterErr5,buffer);

        return buffer;

    }
    public static void setErr(byte[] buffer) {
        laterErr1 =laterErr2;
        laterErr2 =laterErr3;
        laterErr3 =laterErr4;
        laterErr4 =laterErr5;
        laterErr5 =buffer;
    }

    public static int unboxing(byte[] buffer, String recvPackeHex) {


        buffer  =  getErr(buffer);

        PacketParser unboxing = unboxinghead(buffer);// 拆包
        for (byte[] b1 : unboxing.normalList) {
            String hex = Hexs.ByteToHex(b1);
            boolean isPackage = AcPackage.isPackage(b1);

            if (isPackage ) {
                AcPackage ac = new AcPackage(b1);

                int length = Hexs.LowBytesToInt2F(ac.length);

                if (isPackage && length == ac.body.length) {

                    Decode(ac, hex );  // 拆包后，处理每一条报文

                }else {
                    System.out.println("【AC报文】:长度解码错误 "
                            + " frame:0x" + Hexs.ByteToHex(new byte[]{ac.frame})
                            + "  :buffer.length：" + buffer.length
                            +  " :body1.length：" + length
                            +  " :body2.length：" + ac.body.length
                            + " HEX:" + hex
                    );
                }

                //  return ; 不要退出循环

            }
            else {
                System.out.println("【AC报文】:解码错误 "
                        + "  :buffer.length：" + buffer.length
                        + " HEX:" + hex
                );
            }

            System.out.println("normalList:"+isPackage + "  hex:"+hex);
        }


        for (byte[] b1 : unboxing.headList) {

            // 无法处理
            boolean isPackage = AcPackage.isPackage(b1);
            String  hex = Hexs.ByteToHex(b1);
            System.out.println("headList:"+isPackage + "  hex:"+hex);

        }
        setErr(unboxing.laterErr);  // 使用一组变更来保存，防止多次不连贯的报文
        return unboxing.normalList.size();
    }




    // 是否符合头部特征
    public static boolean hex0xaaac(byte[] bytes,int index) {

        if( index==0 && bytes[index] == (byte) 0xaa && bytes[index+1] == (byte) 0xac ){
            return true;
        }

        if( index<bytes.length-2 && bytes[index] == (byte) 0xaa && bytes[index+1] == (byte) 0xac ){
            return true;
        }

        return false;
    }


    // 以开始标记拆包
    public static PacketParser unboxinghead(byte[] bytes) {
        PacketParser packetParser =new  PacketParser();

        int bigen2_index  = 0;

        // 以开始标记
        for (int i = 0 ; i < bytes.length; i++) {
            // 寻找下一个开始
            if(hex0xaaac(bytes,i)){
                //以开始标记
                if(i>bigen2_index){
                    byte[] dest = new byte[i - bigen2_index];
                    System.arraycopy(bytes, bigen2_index, dest, 0, dest.length);

                    boolean isa=dest[0]==(byte) 0xaa && dest[dest.length-1]==0x0a;

                    if(isa){
                        packetParser.normalList.add(dest);
                    }else {
                        packetParser.headList.add(dest);
                    }
                }
                bigen2_index  = i;
            }

            boolean isa3= bigen2_index <  i &&  i == bytes.length-1;


            if(isa3){
                byte[] dest = new byte[bytes.length - bigen2_index];
                System.arraycopy(bytes, bigen2_index, dest, 0, dest.length);
                boolean isa2=dest[0]==(byte) 0xaa && dest[dest.length-1]==0x0a;
                if(isa2){
                    packetParser.normalList.add(dest);
                }else {
                    packetParser.laterErr = dest;
                }

                System.out.println("以开始标记最后一条   :"
                                + " bigen2_index： " + bigen2_index
                                + " bytes.length： " + bytes.length
                                + " i： " + i
//                        + " hex： " + hex
                );
            }


        }
        return packetParser;
    }



    // 处理报文
    public static void Decode(AcPackage acp, String hex) {
        System.out.println("【AC报文】:处理报文 "
                + " frame:0x" + Hexs.ByteToHex(new byte[]{acp.frame})
                + " " + hex

        );
//        read_package(new byte[]{acp.frame});
        byte[] b = hexStringToByteArray2(hex);
        Serialzhi.iDecode.iDecode(b);
    }

}
